/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.IsfdMeshOp2Constants;
import com.comac.ins.isfd.constant.enums.IsfdLargeFileStatusEnum;
import com.comac.ins.isfd.constant.enums.IsfdLargeFileTypeEnum;
import com.comac.ins.isfd.constant.enums.IsfdMeshModelFilteredEnum;
import com.comac.ins.isfd.constant.enums.IsfdMongoQueryOperatorEnum;
import com.comac.ins.isfd.domain.IsfdDatabaseMongoQuery;
import com.comac.ins.isfd.domain.IsfdLargeFile;
import com.comac.ins.isfd.domain.IsfdMeshOp2Collect;
import com.comac.ins.isfd.domain.bo.IsfdLargeFileBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CollectBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CollectConfirmBo;
import com.comac.ins.isfd.domain.bo.query.IsfdMeshOp2CollectQueryBo;
import com.comac.ins.isfd.domain.chunks.ChunksCheckResultVo;
import com.comac.ins.isfd.domain.chunks.ChunksUploadResultVo;
import com.comac.ins.isfd.domain.vo.IsfdLargeFileVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelStructureDefinitionVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CollectHandlingVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CollectVo;
import com.comac.ins.isfd.mapper.IsfdMeshOp2CollectMapper;
import com.comac.ins.isfd.service.*;
import com.comac.ins.isfd.util.SnowflakeIdGenerator;
import com.comac.ins.system.domain.SysUser;
import com.comac.ins.system.domain.vo.SysUserVo;
import com.comac.ins.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 自然网格结果库-采集Service业务层处理
 *
 * @author hxloongs
 * @date 2025-03-24
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class IsfdMeshOp2CollectServiceImpl implements IIsfdMeshOp2CollectService {

    @Autowired
    private IsfdMeshOp2CollectMapper baseMapper;

    @Autowired
    private IIsfdCollectRoundHistoryService iIsfdCollectRoundHistoryService;

    @Autowired
    private IIsfdCollectBatchHistoryService iIsfdCollectBatchHistoryService;

    @Autowired
    private IIsfdLargeFileService isfdLargeFileService;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private IIsfdDatabaseMongoService mongoService;

    @Autowired
    private IIsfdMeshModelStructureDefinitionService iIsfdMeshModelStructureDefinitionService;

    @Autowired
    private IIsfdMeshOP2ParseService isfdMeshOP2ParseService;

    @Override
    public IsfdMeshOp2CollectVo queryById(String id) {
        IsfdMeshOp2CollectVo isfdMeshOp2CollectVo = baseMapper.selectVoOne(new LambdaQueryWrapper<IsfdMeshOp2Collect>().eq(IsfdMeshOp2Collect::getId, id));
        // 大文件
        IsfdLargeFileVo isfdLargeFileVo = isfdLargeFileService.queryById(isfdMeshOp2CollectVo.getUploadId());
        isfdMeshOp2CollectVo.setIsfdLargeFileVo(isfdLargeFileVo);
        return isfdMeshOp2CollectVo;
    }

    @Override
    public TableDataInfo<IsfdMeshOp2CollectVo> queryPageList(IsfdMeshOp2CollectQueryBo queryBo) {
        Wrapper<IsfdMeshOp2Collect> wrapper = buildQueryWrapper(queryBo);
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());
        Page<IsfdMeshOp2CollectVo> result = baseMapper.queryPageList(pageQuery.build(), wrapper);
        for (IsfdMeshOp2CollectVo item : result.getRecords()) {
            if (Objects.nonNull(item)) {
                // 文件上传时间
                IsfdLargeFileVo isfdLargeFileVo = item.getIsfdLargeFileVo();
                if (Objects.nonNull(isfdLargeFileVo.getCreateBy())) {
                    SysUserVo sysUser = userMapper.selectVoOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserId, isfdLargeFileVo.getCreateBy()));
                    isfdLargeFileVo.setCreateByName(sysUser.getNickName());
                }
                String modelNumber = item.getModelNumber();
                String location = modelNumber + "-" + item.getBatchNumber();
                // 处理sectionNames
                StringJoiner sectionJoiner = new StringJoiner("、", "[", "]");
                List<String> sectionNames = new ArrayList<>();
                if (StringUtils.isNotBlank(item.getSectionNumbers())) {
                    Arrays.stream(item.getSectionNumbers().split(","))
                        .map(String::trim)
                        .filter(StringUtils::isNotBlank)
                        .forEach(sectionNumber -> {
                            IsfdMeshModelStructureDefinitionVo sectionVo = iIsfdMeshModelStructureDefinitionService
                                .queryOneByQueryIndex(modelNumber, sectionNumber, null);
                            if (sectionVo != null && StringUtils.isNotBlank(sectionVo.getName())) {
                                sectionJoiner.add(sectionVo.getName());
                                sectionNames.add(sectionVo.getName());
                            }
                        });
                }
                // 生成逗号连接的字符串
                String sectionNumbersString = sectionNames.isEmpty() ? "" : String.join(",", sectionNames);
                item.setSectionNumbersString(sectionNumbersString);
                item.setLocation(location + "-" + sectionJoiner.toString());
            }
        }
        return TableDataInfo.build(result);
    }


    @Override
    public List<IsfdMeshOp2CollectVo> queryList(IsfdMeshOp2CollectBo bo) {
        LambdaQueryWrapper<IsfdMeshOp2Collect> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public IsfdMeshOp2CollectHandlingVo dataHandling(List<IsfdMeshOp2CollectBo> isfdMeshOp2CollectBoList) {
        long totalCount = 0L;
        long notFilterCount = 0L;
        long filterCount = 0L;
        IsfdMeshOp2CollectHandlingVo resultVo = new IsfdMeshOp2CollectHandlingVo();
        List<IsfdMeshOp2CollectVo> isfdMeshOp2CollectVoList = new ArrayList<>();

        String[] op2SuffixStr = IsfdMeshOp2Constants.OP2_SUFFIX_LIST;
        List<String> op2SuffixList = new ArrayList<>(List.of(op2SuffixStr));
        for (IsfdMeshOp2CollectBo isfdMeshOp2CollectBo : isfdMeshOp2CollectBoList) {
            IsfdLargeFileBo isfdLargeFileBo = isfdMeshOp2CollectBo.getIsfdLargeFileBo();
            IsfdMeshOp2CollectVo isfdMeshOp2CollectVo = new IsfdMeshOp2CollectVo();
            BeanUtils.copyProperties(isfdMeshOp2CollectBo, isfdMeshOp2CollectVo);

            IsfdLargeFileVo isfdLargeFileVo = new IsfdLargeFileVo();
            BeanUtils.copyProperties(isfdLargeFileBo, isfdLargeFileVo);

            totalCount = totalCount + 1L;
            // 判断是否过滤
            if (IsfdMeshModelFilteredEnum.FILTERED.getValue().equals(isfdMeshOp2CollectBo.getFiltered())) {
                filterCount = filterCount + 1L;
                isfdMeshOp2CollectVo.setFiltered(IsfdMeshModelFilteredEnum.FILTERED.getValue());
                continue;
            }

            String fileSuffix = isfdLargeFileBo.getFileSuffix();
            if (!op2SuffixList.contains(fileSuffix)) {
                filterCount = filterCount + 1L;
                isfdMeshOp2CollectVo.setFiltered(IsfdMeshModelFilteredEnum.FILTERED.getValue());
                continue;
            }
            isfdMeshOp2CollectVo.setIsfdLargeFileVo(isfdLargeFileVo);
            isfdMeshOp2CollectVoList.add(isfdMeshOp2CollectVo);
            notFilterCount = notFilterCount + 1L;
        }
        resultVo.setTotalCount(totalCount);
        resultVo.setFilterCount(filterCount);
        resultVo.setNotFilterCount(notFilterCount);
        resultVo.setIsfdMeshOp2CollectVoList(isfdMeshOp2CollectVoList);
        return resultVo;
    }

    @Override
    @Transactional
    public List<ChunksUploadResultVo> confirm(IsfdMeshOp2CollectConfirmBo isfdMeshOp2CollectConfirmBo) {
        // 通过雪花生成唯一组id
        SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
        long groupId = idGenerator.nextId();
        isfdMeshOp2CollectConfirmBo.setGroupId(groupId);

        String modelNumber = isfdMeshOp2CollectConfirmBo.getModelNumber();
        String roundNumber = isfdMeshOp2CollectConfirmBo.getRoundNumber();
        List<IsfdLargeFileBo> largeFileBoList = isfdMeshOp2CollectConfirmBo.getLargeFileBoList();
        List<ChunksUploadResultVo> resultVoList = new ArrayList<>();
        for (IsfdLargeFileBo isfdLargeFileBo : largeFileBoList) {
            // 初始化分片上传
            isfdLargeFileBo.setType(IsfdLargeFileTypeEnum.MESH_OP2.getValue());
            IsfdLargeFile isfdLargeFile = isfdLargeFileService.queryByByFileHash(isfdLargeFileBo.getFileHash());
            String uploadId;
            // 判断文件是否已经存在
            Boolean needInertOp2 = Boolean.FALSE;
            if (!Objects.nonNull(isfdLargeFile)) {
                // 新上传文件
                ChunksUploadResultVo resultVo = isfdLargeFileService.chunksUploadInit(isfdLargeFileBo);
                resultVoList.add(resultVo);
                uploadId = resultVo.getUploadId();
            } else {
                // 已有文件
                ChunksUploadResultVo chunksUploadResultVo = new ChunksUploadResultVo();
                uploadId = isfdLargeFile.getUploadId();
                if (IsfdLargeFileStatusEnum.INCOMPLETE.getValue().equals(isfdLargeFile.getStatus())) {
                    ChunksCheckResultVo chunksCheckResultVo = isfdLargeFileService.chunksUploadCheck(isfdLargeFile.getUploadId(), isfdLargeFile.getFileHash());
                    BeanUtils.copyProperties(chunksCheckResultVo, chunksUploadResultVo);
                } else {
                    chunksUploadResultVo.setUploadId(uploadId);
                    chunksUploadResultVo.setFileHash(isfdLargeFile.getFileHash());
                    chunksUploadResultVo.setFileUrl(isfdLargeFile.getFileUrl());
                    chunksUploadResultVo.setStatus(isfdLargeFile.getStatus());
                    needInertOp2 = Boolean.TRUE;
                }
                resultVoList.add(chunksUploadResultVo);
            }
            // 记录采集表信息
            save(isfdMeshOp2CollectConfirmBo, uploadId, isfdLargeFile, needInertOp2);
        }
        iIsfdCollectRoundHistoryService.save(modelNumber, roundNumber);
        // 生成机型批次记录
        iIsfdCollectBatchHistoryService.save(modelNumber, isfdMeshOp2CollectConfirmBo.getBatchNumber());
        return resultVoList;
    }

    @Override
    @Transactional
    public List<ChunksUploadResultVo> reUpload(IsfdMeshOp2CollectConfirmBo isfdMeshOp2CollectConfirmBo) {
        List<ChunksUploadResultVo> resultVoList = new ArrayList<>();
        // 通过文件hash判断是否为重传
        // 只有hash变化，才重新关联上传
        String originalFileHash = isfdMeshOp2CollectConfirmBo.getOriginalFileHash();
        IsfdLargeFile isfdLargeFile = isfdLargeFileService.queryByByFileHash(originalFileHash);
        if (!Objects.nonNull(isfdLargeFile)) {
            throw new BaseException("原文件hash未找到！");
        }
        Long id = isfdMeshOp2CollectConfirmBo.getId();
        if (!Objects.nonNull(id)) {
            throw new BaseException("自然网格结果id未找到！");
        }
        // 新增自然网格文件
        IsfdMeshOp2CollectVo isfdMeshOp2CollectVo = baseMapper.selectVoById(id);
        if (!Objects.nonNull(isfdMeshOp2CollectVo)) {
            throw new BaseException("自然网格未找到！");
        }
        isfdMeshOp2CollectConfirmBo.setGroupId(isfdMeshOp2CollectVo.getGroupId());
        isfdMeshOp2CollectConfirmBo.setModelNumber(isfdMeshOp2CollectVo.getModelNumber());
        isfdMeshOp2CollectConfirmBo.setBatchNumber(isfdMeshOp2CollectVo.getBatchNumber());
        isfdMeshOp2CollectConfirmBo.setSectionNumbers(isfdMeshOp2CollectVo.getSectionNumbers());
        isfdMeshOp2CollectConfirmBo.setRoundNumber(isfdMeshOp2CollectVo.getRoundNumber());
        isfdMeshOp2CollectConfirmBo.setTableName(isfdMeshOp2CollectVo.getTableName());
        isfdMeshOp2CollectConfirmBo.setRemarks(isfdMeshOp2CollectVo.getRemarks());
        // 删除自然网格结果
        baseMapper.delete(new LambdaQueryWrapper<IsfdMeshOp2Collect>().eq(IsfdMeshOp2Collect::getId, id));
        // 保存新的单个文件（业务限制）
        List<IsfdLargeFileBo> largeFileBoList = isfdMeshOp2CollectConfirmBo.getLargeFileBoList();
        IsfdLargeFileBo isfdLargeFileBo = largeFileBoList.get(0);
        // 判断是否已上传过
        isfdLargeFileBo.setType(IsfdLargeFileTypeEnum.MESH_OP2.getValue());
        IsfdLargeFile isfdNewLargeFileByFileHash = isfdLargeFileService.queryByByFileHash(isfdLargeFileBo.getFileHash());
        String uploadIdByNewLargeHash;
        Boolean needInertOp2 = Boolean.FALSE;
        if (Objects.nonNull(isfdNewLargeFileByFileHash)) {
            // 已有文件
            uploadIdByNewLargeHash = isfdNewLargeFileByFileHash.getUploadId();
            ChunksUploadResultVo chunksUploadResultVo = new ChunksUploadResultVo();
            if (IsfdLargeFileStatusEnum.INCOMPLETE.getValue().equals(isfdNewLargeFileByFileHash.getStatus())) {
                ChunksCheckResultVo chunksCheckResultVo = isfdLargeFileService.chunksUploadCheck(uploadIdByNewLargeHash, isfdNewLargeFileByFileHash.getFileHash());
                BeanUtils.copyProperties(chunksCheckResultVo, chunksUploadResultVo);
            } else {
                chunksUploadResultVo.setUploadId(uploadIdByNewLargeHash);
                chunksUploadResultVo.setFileHash(isfdNewLargeFileByFileHash.getFileHash());
                chunksUploadResultVo.setFileUrl(isfdNewLargeFileByFileHash.getFileUrl());
                chunksUploadResultVo.setStatus(isfdNewLargeFileByFileHash.getStatus());
                needInertOp2 = Boolean.TRUE;
            }
            resultVoList.add(chunksUploadResultVo);
        } else {
            // 没有文件，需要上传
            ChunksUploadResultVo resultVo = isfdLargeFileService.chunksUploadInit(isfdLargeFileBo);
            resultVoList.add(resultVo);
            uploadIdByNewLargeHash = resultVo.getUploadId();
        }
        // 记录采集表信息
        isfdMeshOp2CollectConfirmBo.setId(null);
        save(isfdMeshOp2CollectConfirmBo, uploadIdByNewLargeHash, isfdNewLargeFileByFileHash, needInertOp2);
        return resultVoList;
    }

    private LambdaQueryWrapper<IsfdMeshOp2Collect> buildQueryWrapper(IsfdMeshOp2CollectBo bo) {
        LambdaQueryWrapper<IsfdMeshOp2Collect> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUploadTime() != null, IsfdMeshOp2Collect::getUploadTime, bo.getUploadTime());
        lqw.eq(StringUtils.isNotBlank(bo.getModelNumber()), IsfdMeshOp2Collect::getModelNumber, bo.getModelNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNumber()), IsfdMeshOp2Collect::getBatchNumber, bo.getBatchNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSectionNumbers()), IsfdMeshOp2Collect::getSectionNumbers, bo.getSectionNumbers());
        lqw.eq(StringUtils.isNotBlank(bo.getRoundNumber()), IsfdMeshOp2Collect::getRoundNumber, bo.getRoundNumber());
        lqw.eq(bo.getQualityCheck() != null, IsfdMeshOp2Collect::getQualityCheck, bo.getQualityCheck());
        lqw.eq(StringUtils.isNotBlank(bo.getMessage()), IsfdMeshOp2Collect::getMessage, bo.getMessage());
        return lqw;
    }

    private Wrapper<IsfdMeshOp2Collect> buildQueryWrapper(IsfdMeshOp2CollectQueryBo bo) {
        QueryWrapper<IsfdMeshOp2Collect> lqw = Wrappers.query();
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), "f.file_name", bo.getFileName())
            .eq(StringUtils.isNotBlank(bo.getModelNumber()), "c.model_number", bo.getModelNumber())
            .eq(StringUtils.isNotBlank(bo.getBatchNumber()), "c.batch_number", bo.getBatchNumber())
            .like(StringUtils.isNotBlank(bo.getSectionNumber()), "c.section_numbers", bo.getSectionNumber())
            .eq(StringUtils.isNotBlank(bo.getRoundNumber()), "c.round_number", bo.getRoundNumber())
            .eq(StringUtils.isNotBlank(bo.getFileSuffix()), "c.file_suffix", bo.getFileSuffix())
            .orderByDesc("c.create_time");
        if (bo.getUploadTimeStartTime() != null && bo.getUploadTimeEndTime() != null) {
            lqw.between("c.upload_time", bo.getUploadTimeStartTime().getTime(), bo.getUploadTimeEndTime().getTime());
        }
        return lqw;
    }

    private void save(IsfdMeshOp2CollectConfirmBo isfdMeshOp2CollectConfirmBo, String uploadId,
                      IsfdLargeFile isfdLargeFile, Boolean needInertOp2) {
        long uploadTime = System.currentTimeMillis();
        String remarks = isfdMeshOp2CollectConfirmBo.getRemarks();
        // 采集表的唯一判断
        // 是机型-批次-轮次-部段 文件hash一致的话，就不生成采集记录，只更新uploadTime和备注 且要更新 结果库的 更新uploadTime和备注
        String modelNumber = isfdMeshOp2CollectConfirmBo.getModelNumber();
        String batchNumber = isfdMeshOp2CollectConfirmBo.getBatchNumber();
        String roundNumber = isfdMeshOp2CollectConfirmBo.getRoundNumber();
        String sectionNumbers = isfdMeshOp2CollectConfirmBo.getSectionNumbers();
        // 重新拼接成字符串
        sectionNumbers = sortSectionNumber(sectionNumbers);
        IsfdMeshOp2Collect one = baseMapper.selectOne(new LambdaQueryWrapper<IsfdMeshOp2Collect>()
            .eq(IsfdMeshOp2Collect::getModelNumber, modelNumber)
            .eq(IsfdMeshOp2Collect::getBatchNumber, batchNumber)
            .eq(IsfdMeshOp2Collect::getRoundNumber, roundNumber)
            .eq(IsfdMeshOp2Collect::getSectionNumbers, sectionNumbers)
            .eq(IsfdMeshOp2Collect::getUploadId, uploadId));
        if (one != null) {
            one.setUploadTime(uploadTime);
            one.setRemarks(remarks);
            baseMapper.updateById(one);

            IsfdDatabaseMongoQuery queryCondition = new IsfdDatabaseMongoQuery();
            IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition condition = new IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition();
            condition.setFieldName("uploadId");
            condition.setValue(uploadId);
            condition.setOperator(IsfdMongoQueryOperatorEnum.EQUAL.getValue());
            queryCondition.setConditions(Collections.singletonList(condition));
            Map<String, Object> update = new HashMap<>();
            update.put("uploadTime", uploadTime);
            update.put("remarks", remarks);
            mongoService.update(one.getTableName(), queryCondition, update);
        } else {
            IsfdMeshOp2Collect isfdMeshOp2Collect = new IsfdMeshOp2Collect();
            isfdMeshOp2Collect.setUploadId(uploadId);
            isfdMeshOp2Collect.setModelNumber(modelNumber);
            isfdMeshOp2Collect.setBatchNumber(batchNumber);
            isfdMeshOp2Collect.setRoundNumber(roundNumber);
            isfdMeshOp2Collect.setSectionNumbers(sectionNumbers);
            isfdMeshOp2Collect.setTableName(isfdMeshOp2CollectConfirmBo.getTableName());
            isfdMeshOp2Collect.setRemarks(remarks);
            isfdMeshOp2Collect.setFiltered(IsfdMeshModelFilteredEnum.NOT_FILTERED.getValue());
            isfdMeshOp2Collect.setUploadTime(uploadTime);
            isfdMeshOp2Collect.setGroupId(isfdMeshOp2CollectConfirmBo.getGroupId());
            baseMapper.insert(isfdMeshOp2Collect);

            if (needInertOp2) {
                isfdLargeFileService.meshOp2Insert(isfdMeshOp2Collect, isfdLargeFile);
            }
        }
    }


    /**
     * 选择的部段需要排序
     */
    private String sortSectionNumber(String sectionNumbers) {
        String[] strSectionNumbers = sectionNumbers.split(",");
        // 自定义排序
        Arrays.sort(strSectionNumbers, (a, b) -> {
            boolean aIsS = a.startsWith("S") && a.length() > 1 && Character.isDigit(a.charAt(1));
            boolean bIsS = b.startsWith("S") && b.length() > 1 && Character.isDigit(a.charAt(1));

            if (aIsS && bIsS) {
                // 都是S开头的数字，比较数字部分
                int numA = Integer.parseInt(a.substring(1));
                int numB = Integer.parseInt(b.substring(1));
                return Integer.compare(numA, numB);
            } else if (aIsS) {
                // a是S开头数字，b不是，a排在前面
                return -1;
            } else if (bIsS) {
                // b是S开头数字，a不是，b排在前面
                return 1;
            } else {
                // 都不是S开头数字，按字符串自然顺序比较
                return a.compareTo(b);
            }
        });

        sectionNumbers = String.join(",", strSectionNumbers);
        return sectionNumbers;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByUploadId(String uploadId) {

        // 删除大文件记录
        IsfdLargeFileVo largeFileVo = isfdLargeFileService.queryById(uploadId);
        if(largeFileVo != null) {
            String fileHash = largeFileVo.getFileHash();
            isfdLargeFileService.deleteLargeFile(fileHash);
        }

        // 删除采集记录
        List<IsfdMeshOp2Collect> op2CollectList = baseMapper.selectList(new LambdaQueryWrapper<IsfdMeshOp2Collect>()
            .eq(IsfdMeshOp2Collect::getUploadId, uploadId));

        List<String> tableNames = op2CollectList.stream()
            .map(IsfdMeshOp2Collect::getTableName)
            .filter(Objects::nonNull)
            .distinct()
            .toList();

        baseMapper.delete(new LambdaQueryWrapper<IsfdMeshOp2Collect>()
                .eq(IsfdMeshOp2Collect::getUploadId, uploadId));

        // 删除结果库关联数据
        Map<String, Object> deleteFilter = new HashMap<>();
        deleteFilter.put("uploadId", uploadId);
        for (String tableName : tableNames) {

            mongoService.delete(tableName, deleteFilter);
        }

        // 删除解析记录
        isfdMeshOP2ParseService.deleteOp2ParseRecords(uploadId);

        return Boolean.TRUE;
    }

}
